package com.liang.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;

import java.util.*;

/**
 * @ClassName ExcelCopy
 * @Description TODO
 * @Author liang
 * @Date 2019/7/4 16:05
 * @Version 1.0
 **/
public class ExcelCopy {

    ArrayList<Map<String, Object>> rows = CollUtil.newArrayList();


    /**
     * 文件列表 逐个获取内容,放入map
     * @param fileList
     * @return
     */
    public ArrayList<Map<String, Object>> excelToMap(ArrayList<String> fileList,String descPath){
        //遍历文件列表
        if(fileList.size() == 0){
            return null;
        }
        ExcelWriter writer = null;
        for(String path:fileList){
            //获取文件内容
           excelToMap(path);
        }

        //写入目标文件
        //写数据
        writer = ExcelUtil.getWriter(descPath);
        writer.write(rows);
        if(rows.size() == 0){
            new Exception("无数据待写入");
        }
        //设置列宽
        writer.setColumnWidth(0,12);
        writer.setColumnWidth(1,8);
        writer.setColumnWidth(2,4);
        writer.setColumnWidth(3,8);
        writer.setColumnWidth(4,55);
        writer.setColumnWidth(5,10);
        writer.setColumnWidth(6,8);
        CellStyle cellStyle = writer.getCellStyle();
        //设置无边框
        cellStyle.setBorderBottom(BorderStyle.NONE);
        cellStyle.setBorderLeft(BorderStyle.NONE);
        cellStyle.setBorderRight(BorderStyle.NONE);
        cellStyle.setBorderTop(BorderStyle.NONE);

        writer.flush();
        writer.close();
        return rows;
    }

    private void excelToMap(String path) {
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(path));
        List<Map<String,Object>> readAll = reader.readAll();
        //按顺序 筛选当天数据
        String today= TimeUtils.fmToday();
        for (Map<String,Object> map : readAll){
            Map<String, Object> row = new LinkedHashMap<String, Object>();
            try{
                String date = (String) map.get("日期");
                if(date.isEmpty()){
                    row.put("日期","");
                    row.put("姓名",map.get("姓名"));
                    row.put("序号",map.get("序号"));
                    row.put("发件人",map.get("发件人"));
                    row.put("邮件主题",map.get("邮件主题"));
                    row.put("处理状态",map.get("处理状态"));
                    row.put("备注",map.get("备注"));
                    rows.add(row);
                    continue;
                }
                String formatDay = TimeUtils.fmtDate(date);
                if(today.equals(formatDay)){
                    row.put("日期",formatDay);
                    row.put("姓名",map.get("姓名"));
                    row.put("序号",map.get("序号"));
                    row.put("发件人",map.get("发件人"));
                    row.put("邮件主题",map.get("邮件主题"));
                    row.put("处理状态",map.get("处理状态"));
                    row.put("备注",map.get("备注"));
                    rows.add(row);
                }
            }catch (ClassCastException e){
                row.put("日期",e.getMessage());
                row.put("姓名",map.get("姓名"));
                row.put("序号",map.get("序号"));
                row.put("发件人",map.get("发件人"));
                row.put("邮件主题",map.get("邮件主题"));
                row.put("处理状态",map.get("处理状态"));
                row.put("备注",map.get("备注"));
                rows.add(row);
            }
        }
    }

}
